Passed
Pull Request — main (#18)
by Dante
01:29
created

ApiProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 25
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A get 0 21 3
A remove 0 12 2
A debugCoverage 0 8 1
1
import { BEditaApiClient, ApiClientConfig } from './bedita-api-client';
2
3
/**
4
 * ApiProvider is responsible to create a `BEditaApiClient` and return the instance.
5
 *
6
 */
7
export default class ApiProvider {
8
9
    /**
10
     * A map of clients created.
11
     */
12
    static #registry: { [s: string]: BEditaApiClient } = {};
13
14
    /**
15
     * Get an API client already created or try to create new one.
16
     * If a client registered with `name` is found then returns it.
17
     *
18
     * @param name The name to use for register the API client
19
     * @param config The configuration to use for create new client.
20
     */
21
    public static get(name: string, config?: ApiClientConfig): BEditaApiClient {
22
        if (this.#registry[name]) {
23
            return this.#registry[name];
24
        }
25
26
        if (!config || !config.baseUrl) {
27
            throw new Error('Missing required API configuration');
28
        }
29
30
        config.name = name;
31
        this.#registry[name] = new BEditaApiClient(config);
32
33
        return this.#registry[name];
34
    }
35
36
    /**
37
     * Remove an API client istance from the registry.
38
     *
39
     * @param name The name to look for in the registry
40
     */
41
    public static remove(name: string): void {
42
        if (!this.#registry[name]) {
43
            return;
44
        }
45
46
        delete this.#registry[name];
47
    }
48
49
    /**
50
     * Debug function to test coverage
51
     *
52
     * @returns boolean
53
     */
54
    public static debugCoverage(): boolean {
55
        return false;
56
    }
57
}
58